package com.clps.rulesregulations.controller;

import com.clps.rulesregulations.domain.Dept;
import com.clps.rulesregulations.service.DeptService;
import com.clps.rulesregulations.service.ShiroService;
import com.clps.rulesregulations.util.Const;
import com.clps.rulesregulations.util.ResourceTree;
import com.clps.rulesregulations.util.ResultView;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制层
 *
 */
@Controller
@RequestMapping(value = "/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private ShiroService shiroService;

    /**
     * 跳转到菜单展示页
     *
     * @return
     */
    @GetMapping("/list")
    @RequiresRoles("管理员")
    public String toList() {
        return "manager/dept";
    }

    /**
     * 分页查询所有菜单
     *
     * @param condition
     * @return
     * @throws Exception
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultView list(@RequestBody Map<String, Object> condition) throws Exception {

        ResultView resultView = new ResultView(ResultView.SUCCESS);
        PageInfo<Dept> users = deptService.findDeptsByPage(condition);
        resultView.setValue(users);
        return resultView;
    }

    /**
     * 添加菜单
     *
     * @param dept
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultView add(@RequestBody Dept dept) {

        ResultView resultView = new ResultView(ResultView.SUCCESS);
        if (StringUtils.isBlank(dept.getName()) || StringUtils.isBlank(String.valueOf(dept.getParentId()))) {
            resultView.setStatus(ResultView.ERROR);
            resultView.setMsg("请填入必填项");
            return resultView;
        }

        dept.setCanDel(Const.CANDEL);
        deptService.addDept(dept);
        return resultView;
    }

    /**
     * 删除资源
     *
     * @param deptId
     * @return
     */
    @CacheEvict(cacheNames = "depts", allEntries = true)
    @GetMapping("/delete")
    @ResponseBody
    public ResultView delDept(@RequestParam Integer deptId) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        List<Dept> deptList = deptService.selectAll();
        List<String> delList = new ArrayList();
        delList.add(String.valueOf(deptId));
        if (deptList != null && deptList.size() > 0) {
            for (int i = 0; i < deptList.size(); i++) {
                Dept rs = deptList.get(i);
                String fullUrl = rs.getFullUrl();
                if (fullUrl != null) {
                    String[] arr = fullUrl.split(",");
                    for (int k = 0; k < arr.length; k++) {
                        String value = arr[k];
                        if (deptId == Integer.valueOf(value)) {
                            delList.add(String.valueOf(rs.getId()));

                            continue;
                        }
                    }
                }

            }
        }
        if (delList != null && delList.size() > 0) {
            String[] strings = new String[delList.size()];
            delList.toArray(strings);
            deptService.deleteAll(strings);
        }

//        shiroService.updatePermission();
        return resultView;
    }

    /**
     * 根据资源ID查询资源内容
     *
     * @param deptId
     * @return
     */
    @GetMapping("/edit")
    @ResponseBody
    public ResultView findDept(@RequestParam Integer deptId) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        Dept dept = deptService.findDeptById(deptId);
        if (dept != null) {
            String fullUrl = dept.getFullUrl();
            if (!StringUtils.isEmpty(fullUrl)) {
                String[] arr = fullUrl.split(",");
                Integer[] iarr = new Integer[arr.length];
                for (int i = 0; i < arr.length; i++) {
                    iarr[i] = Integer.valueOf(arr[i]);
                }
                dept.setFullUrlArr(iarr);
            }
        }
        resultView.setValue(dept);
        return resultView;
    }

    /**
     * 修改资源
     *
     * @param dept
     * @return
     */
    @CacheEvict(cacheNames = "depts", allEntries = true)
    @PostMapping("/edit")
    @ResponseBody
    public ResultView editDept(@RequestBody Dept dept) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        String fullUrl = dept.getFullUrl();
        String[] arr = fullUrl.split(",");
        String parentId = arr[arr.length - 1];
        dept.setParentId(Integer.valueOf(parentId));
        deptService.updateDept(dept);
//        shiroService.updatePermission();
        return resultView;
    }

    /**
     * 查询所有菜单
     *
     * @return
     */

    @GetMapping("/all")
    @ResponseBody
    public ResultView findAllMenu(Dept rs) {
        Subject currentUser = SecurityUtils.getSubject();
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        Map<String, Object> map = new HashMap<String, Object>();
//        if (currentUser.hasRole("管理员")) {
        List<ResourceTree> menuDbs = deptService.findAllAsTree();

        if (menuDbs.size() > 0) {
            List<ResourceTree> menus = menuDbs.get(0).getChildren();
            map.put("menus", menus);
            map.put("allMenus", menuDbs);
        }
//        }
//        List<DeptTree> regularMenuDbs = deptService.findMenuTree();
//        if (regularMenuDbs != null && regularMenuDbs.size() > 0) {
//            if (regularMenuDbs.get(0).getChildren() != null && regularMenuDbs.get(0).getChildren().size() > 0) {
//                map.put("regulationMenus", regularMenuDbs.get(0).getChildren().get(0).getChildren());
//            }
//        }

//        map.put("defaultIndex", index);

        resultView.setValue(map);
        return resultView;
    }
}
